package drawingBoard.myPanel;

import drawingBoard.ImageFrame;
import drawingBoard.action.ImageAction;
import javax.swing.*;
import java.awt.*;

import static java.awt.Color.*;
import static java.awt.Color.YELLOW;

/**
 * ColorPanel 与 ToolPanel 地位相近
 */
public class ColorPanel extends MyPanel{

    private JPanel currentColorPanel;  //当前颜色面板
    private JToolBar toolBar;  //存放2行6列的色块
    private JPanel colorPanel;  //目标返回值

    @Override
    public void createPanel() {  //创建颜色显示面板
        colorPanel = new JPanel();
        colorPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        toolBar = new JToolBar("颜色");
        toolBar.setMargin(new Insets(2, 2, 2, 2));
        toolBar.setLayout(new GridLayout(2, 6, 2, 2));
    }

    @Override
    public void createAllButton(ImageFrame imageFrame){  //创建全部按钮
        Color[] colorArr = { BLACK, BLUE, CYAN, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW };
        JButton[] panelArr = new JButton[colorArr.length];
        currentColorPanel = new JPanel();
        currentColorPanel.setBackground(Color.BLACK);  //设置默认颜色为黑色
        currentColorPanel.setPreferredSize(new Dimension(20, 20));  //设置色块大小
        for (int i = 0; i < panelArr.length; i++) {
            //创建预设了行为的按钮，按钮单击事件处理的另一种方式
            panelArr[i] = new JButton(new ImageAction(colorArr[i], currentColorPanel));

            panelArr[i].setBackground(colorArr[i]);
            toolBar.add(panelArr[i]);
        }
        colorPanel.add(currentColorPanel);
        colorPanel.add(toolBar);
    }

    @Override
    public JPanel getPanel(){  //获取Panel
        return colorPanel;
    }

    public JPanel getCurrentColorPanel() {   //普通方法
    	return currentColorPanel;
    }
}
